package com.cls.business.service.impl;

import com.cls.common.entity.QueryRequest;
import com.cls.business.entity.LeaderProject;
import com.cls.business.mapper.LeaderProjectMapper;
import com.cls.business.service.ILeaderProjectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import lombok.RequiredArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

/**
 * 主持人研究项目情况表 Service实现
 *
 * @author wandering
 * @date 2020-11-12 15:25:57
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class LeaderProjectServiceImpl extends ServiceImpl<LeaderProjectMapper, LeaderProject> implements ILeaderProjectService {

    @Override
    public PageInfo<LeaderProject> findLeaderProjects(QueryRequest request, LeaderProject leaderProject) {
        LambdaQueryWrapper<LeaderProject> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        if(leaderProject.getDeclareId() != null){
            queryWrapper.eq(LeaderProject::getDeclareId,leaderProject.getDeclareId());
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<LeaderProject> list= this.list(queryWrapper);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<LeaderProject> findLeaderProjects(LeaderProject leaderProject) {
	    LambdaQueryWrapper<LeaderProject> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
        if(leaderProject.getDeclareId() != null){
            queryWrapper.eq(LeaderProject::getDeclareId,leaderProject.getDeclareId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createLeaderProject(LeaderProject leaderProject) {
        this.save(leaderProject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateLeaderProject(LeaderProject leaderProject) {
        this.saveOrUpdate(leaderProject);
    }


    /**
    * 删除
    * @param ids
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteLeaderProjects(String[] ids) {
        List< String > list = Arrays.asList(ids);
        this.removeByIds(list);
     }

    @Override
    public List<LeaderProject> findByDeclareIdAndApplicantId(Long declareId, Long applicantId) {
        return this.baseMapper.findByDeclareIdAndApplicantId(declareId, applicantId);
    }

}
